package com.hanma.dao.impl;

import com.hanma.bean.News;
import com.hanma.bean.Record;
import com.hanma.dao.RecordDao;
import com.hanma.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RecordDaoImpl implements RecordDao {

    @Override
    public int insert(Record record) {
        Connection conn = DBUtil.open();
        int num=0;
        String sql="insert into record(uid,nid,title)  values(?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, record.getUid());
            ps.setInt(2, record.getNid());
            ps.setString(3, record.getTitle());
            num=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(null, ps, conn);
        }
        return num;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public Record find(Integer id) {
        return null;
    }

    @Override
    public List<Record> findAll(Integer uid) {
        Connection conn = DBUtil.open();
        String sql="select * from record where uid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Record> recordList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer nid=rs.getInt("nid");
                String title=rs.getString("title");
                Record record = new Record(uid,nid,title);
                recordList.add(record);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return recordList;
    }

    @Override
    public List<Record> findAll(Integer uid, Integer start, Integer size) {
        Connection conn = DBUtil.open();
        String sql="select * from record where uid=? limit ?,?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Record> recordList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            ps.setInt(2, start);
            ps.setInt(3, size);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer nid=rs.getInt("nid");
                String title=rs.getString("title");
                Record record = new Record(uid,nid,title);
                recordList.add(record);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return recordList;
    }
}
